package com.commit.service.impl;

import com.commit.domain.Likes;
import com.commit.domain.User;
import com.commit.mapper.LikeMapper;
import com.commit.service.LikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * @author:zyh
 * @Time:2021-02-28-15:35
 * @email:1269231889@qq.com
 */
@Service
public class LikeServiceImpl implements LikesService {
    @Autowired
    private LikeMapper likeMapper;
    @Override
    public String save(Likes likes, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if(user==null)  return "unLogin";
        Likes one = likeMapper.findOne(user.getId(), likes.getUserId());
        //likes为空，说明是点赞，不为空，说明是取消点赞s
        if(one==null){
            likes.setUserId(user.getId());
            likeMapper.insert(likes);
            return "success";
        }else{
            likeMapper.del(user.getId(),likes.getAnswerId());
            return "cancel";
        }
    }
}
